package org.example;

import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.my.stock.model.MMaiDian;
import org.my.stock.util.BrotliHttpClientHelper;
import org.my.stock.util.MyUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DelMaiDian {

    @Test
    public void test_1() throws SQLException, ClassNotFoundException {
        Connection conn = MyUtil.getConnection();

        String sql = "SELECT symbol FROM type_stock GROUP BY symbol";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            del_mai_dian(conn, rs.getString(1));
            System.out.println(rs.getString(1));
        }

        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
    }

    @Test
    public void test_2() throws SQLException, ClassNotFoundException {
        Connection conn = MyUtil.getConnection();
        del_mai_dian(conn, "688577");
    }

    public void del_mai_dian(Connection conn, String symbol) throws SQLException {
        String sql = "select id from mai_dian where symbol = '"+ symbol +"' ORDER BY STOCK_DATE DESC";
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Long> lst = new ArrayList<>();

        try {
            stmt = conn.prepareStatement(sql);

            // 5. 执行查询并获取结果集
            rs = stmt.executeQuery();

            // 6. 遍历结果集并处理数据
            while (rs.next()) {
                //id = rs.getLong(1);
                lst.add(rs.getLong(1));
            }

        } catch (SQLException e) {
            System.err.println("数据库操作失败！错误信息：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (lst.size() >= 12) {
            for (int i = 12; i < lst.size(); i++) {
                String sql1 = "DELETE FROM mai_dian WHERE id = " + lst.get(i).toString();
                //cache.query(new SqlFieldsQuery(sql)).getAll();
                Statement stmt1 = conn.createStatement();
                stmt1.execute(sql1);
            }
        }
    }

}










































